package chuichui.resource
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	[EditorData(extensions="mp3")]

	/**
	 * 加载声音
	 * @author hyy
	 *
	 */
	public class MP3Resource extends Resource
	{
		public var sound : Sound;

		public function MP3Resource(url : String, returnFun : Function, errorFun : Function = null)
		{
			super(url, returnFun,errorFun);
		}

		override public function load(context : LoaderContext = null) : void
		{
			if(context == null)
			{
				context = new LoaderContext();
				context.applicationDomain = ApplicationDomain.currentDomain;
			}
			sound = new Sound();
			sound.addEventListener(ProgressEvent.PROGRESS, onProgressEvent);
			sound.addEventListener(Event.COMPLETE, onDownLoadComplete);
			sound.addEventListener(IOErrorEvent.IO_ERROR, onDownloadError);
			sound.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onDownloadSecurityError);
			sound.load(request);
		}

		override public function stop() : void
		{
			try
			{
				if(sound)
				{
					sound.close();
				}
			}
			catch(e : Error)
			{
			}
			super.stop();
		}

		/**
		 * 清除监听
		 *
		 */
		override public function cleanListeners() : void
		{
			if(!sound)
				return;
			sound.removeEventListener(ProgressEvent.PROGRESS, onProgressEvent);
			sound.removeEventListener(Event.COMPLETE, onDownLoadComplete);
			sound.removeEventListener(IOErrorEvent.IO_ERROR, onDownloadError);
			sound.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onDownloadSecurityError);
		}

		override public function destory() : void
		{
			this.sound = null;
			super.destory();
		}

	}
}